package com.etv.launcher.ui.actvity.preference.application;

import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.etv.launcher.R;
import com.etv.launcher.databinding.FragmentPreferenceApplicationBinding;

import java.util.Objects;

import dagger.hilt.android.AndroidEntryPoint;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AndroidEntryPoint
public class PreferenceApplicationFragment extends Fragment {

    private String applicationId;

    private FragmentPreferenceApplicationBinding binding;

    private PackageManager packageManager;

    public PreferenceApplicationFragment(String applicationId){
        this.applicationId = applicationId;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        binding = FragmentPreferenceApplicationBinding.inflate(inflater, container, false);
        packageManager = requireContext().getPackageManager();
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.setDrawlayoutView();
        this.setListener();
    }

    /**
     * 跳转fragment
     * @param fragment
     */
    private void changeFragment(Fragment fragment){
        FragmentTransaction transaction = getParentFragmentManager().beginTransaction();
        transaction.setCustomAnimations(
                R.anim.slide_from_right,  // 第二个Fragment从右往左进入
                0,  // 第一个Fragment不需要退出动画
                0,  // 返回时第一个Fragment不需要进入动画
                R.anim.slide_to_right   // 返回时第二个Fragment从左往右退出
        );
        transaction.add(R.id.preference_container, fragment);  // 使用add()而不是replace()
        transaction.addToBackStack("tag");
        transaction.commit();
    }


    public boolean onBackPressed() {
        FragmentManager fragmentManager = getChildFragmentManager();
        if (fragmentManager.getBackStackEntryCount() > 0) {
            // 如果侧边栏的Fragment栈不为空，则执行返回逻辑
            fragmentManager.popBackStack();
            return true;  // 已处理返回逻辑
        }
        return false;  // 未处理返回逻辑
    }

    private void setDrawlayoutView() {
        try {
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(this.applicationId, 0);
            binding.appName.setText(packageManager.getApplicationLabel(applicationInfo));
        }catch (Exception e){

        }

    }


    private void setListener(){
        binding.uninstallApp.setOnClickListener(v -> {
            Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
            uninstallIntent.setData(Uri.parse("package:" + this.applicationId));
            uninstallIntent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
            startActivityForResult(uninstallIntent, 1);
        });

        binding.appAbout.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setAction(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            Uri uri = Uri.fromParts("package", this.applicationId, null);
            intent.setData(uri);
            startActivity(intent);
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

}